// pages/noteedit/noteedit.js
wx.cloud.init()//初始化云服务器
import request from '../../utils/request'
import {$toast} from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    notecontent:'',
    notetitle:'',
    noteimg:'',
    fileList:[],
    show:false,
    actions:[{name:'美食'},{name:'旅行'},{name:'汽车'},{name:'时尚'},{name:'科技'},],
    note_type:'',
  },
  publish(){
    let { nickname,id} = wx.getStorageSync('userinfo')
    if(!this.data.notecontent || !this.data.notetitle || !this.data.note_type){
      $toast('请补齐内容','error')
      return
    }
    request('/diary/publishnote','post',{
      note_content:this.data.notecontent,
      title:this.data.notetitle,
      head_img:this.data.noteimg,
      note_type:this.data.note_type,
      nickname:nickname,
      userid:id
    }).then(res =>{
      // console.log(res.data.mess);
      $toast(res.data.mess, res.data.code == 200? 'success':'error')
      setTimeout(()=>{
        wx.navigateBack({
          delta: 1,
        })
      },1000)
      
      
    })
  },
  onteimg(e){
    console.log(e.detail.file.url);
    wx.cloud.uploadFile({//小程序上传资源
      cloudPath:`${new Date().getTime()}.png`,
      filePath:e.detail.file.url
    }).then(res=>{
      // console.log(res.fileID);
      let fileList = this.data.fileList
      fileList.push({url:res.fileID})
      this.setData({
        noteimg:res.fileID,
        fileList:fileList
      })

    })
  },
  noteimgdel(e){
    // console.log(e.detail.file);
    let filelist = this.data.fileList
    filelist.splice(e.detail.index)
    this.setData({
      fileList:filelist
    })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  onshowAction(){
    this.setData({
      show:true
    })
  },
  onSelect(e){
    // console.log(e.detail.name);
    this.setData({
      note_type:e.detail.name,
      show:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  notecontent(e){
    // console.log(e);
    this.setData({
      notecontent:e.detail.html
    })
  },
  notetitle(e){
    // console.log(e.detail);
    this.setData({
      notetitle:e.detail,
      noteimg:''
    })
    
  }, 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})